package com.xf.web.controller.sys;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xf.pm.util.MD5;
import com.xf.sys.entity.SysUser;
import com.xf.sys.service.ISysMenuService;
import com.xf.web.util.Constants;
import com.xf.web.util.SessionUtil;
/**
 *LoginController.java
 *LoginController
 *author:lb
 *date:2016年10月4日
 */
@Controller
public class LoginController {
	
	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ISysMenuService sysMenuService;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(HttpServletRequest request)
	{
		return "login";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String index(HttpServletRequest request,SysUser sysUser)
	{
		// 想要得到 SecurityUtils.getSubject() 的对象．．访问地址必须跟shiro的拦截地址内．不然后会报空指针
	    Subject sub = SecurityUtils.getSubject();
	    // 用户输入的账号和密码,,存到UsernamePasswordToken对象中..然后由shiro内部认证对比,
 		// 认证执行者交由ShiroDbRealm中doGetAuthenticationInfo处理
 		// 当以上认证成功后会向下执行,认证失败会抛出异常
	    String loginPwd=MD5.digest(sysUser.getLoginUser()+sysUser.getLoginPwd()+"xf");
	    UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getLoginUser(),loginPwd);
		try {
			sub.login(token);
		}catch (LockedAccountException lae){
			token.clear();
			request.setAttribute("LOGIN_ERROR_CODE",Constants.LOGIN_ERROR_CODE_100002);
			request.setAttribute("LOGIN_ERROR_MESSAGE",Constants.LOGIN_ERROR_MESSAGE_SYSTEMERROR);
			return "login";
		} catch (ExcessiveAttemptsException e){
			token.clear();
			request.setAttribute("LOGIN_ERROR_CODE",Constants.LOGIN_ERROR_CODE_100003);
			request.setAttribute("LOGIN_ERROR_MESSAGE","账号：" + sysUser.getLoginUser()+ Constants.LOGIN_ERROR_MESSAGE_MAXERROR);
			return "login";
		} catch (AuthenticationException e){
			token.clear();
			request.setAttribute("LOGIN_ERROR_CODE",Constants.LOGIN_ERROR_CODE_100001);
			request.setAttribute("LOGIN_ERROR_MESSAGE",	Constants.LOGIN_ERROR_MESSAGE_USERERROR);
			return "login";
		}
		logger.info("登录成功:"+sysUser.getLoginUser());
		return "redirect:/index.do";
	}
	
	@RequestMapping("index")
	public String index(Model model, HttpServletRequest request)
	{
		model.addAttribute("list",sysMenuService.listTree(SessionUtil.getSession().getRoleId()));
		return "index";
	}
	
}
